//
//  HMStatusCell.swift
//  Weibo
//
//  Created by 李学林 on 15/12/10.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
import SnapKit
let HMStatusCellMargin: CGFloat = 10

class HMStatusCell: UITableViewCell {
    
    var toolBarTopConstraint: Constraint?
    
    var statusViewModel: HMStatusViewModel? {
    
        didSet{

            originalView.statusViewModel = statusViewModel
            // 给Toolbar传递模型
            statusToolBar.statusViewModel = statusViewModel
            
            
            toolBarTopConstraint?.uninstall()
            // SnapKit 更新约束适用于
            if statusViewModel?.status?.retweeted_status != nil {
                // 转发微博存在.
                // 需要将其显示.因为cell是可以复用的
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                // 更新约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else {
                // 转发微博不存在
                retweetView.hidden = true
                // 更新约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        // 为了显示每个cell之间的的间条
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        // 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(statusToolBar)
        // 添加约束
        // 1. 原创微博的约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)

            // 在这里拿不到contentText 因此写到原创微博中
        
        // 2. 转发微博的约束
        retweetView.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
//            make.height.equalTo(44)
        })
            
        // 3. 转发边栏约束
        statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                make.leading.equalTo(contentView)
                make.trailing.equalTo(contentView)
                make.height.equalTo(35)
            })
            
        // 添加contentView的约束---------重要
            contentView.snp_makeConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(statusToolBar)
                make.top.equalTo(self)
                make.leading.equalTo(self)
                make.right.equalTo(self)
            })
            
        }
        
    }
    
//    <SnapKit.LayoutConstraint:0x7f80c2f44d00@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#137 Weibo.HMPictureView:0x7f80c3939800.height == 235.0>",
//    "<SnapKit.LayoutConstraint:0x7f80c2f78d70@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#137 Weibo.HMPictureView:0x7f80c3939800.top == UILabel:0x7f80c2fe3ea0.bottom + 10.0>",
//    "<SnapKit.LayoutConstraint:0x7f80c51494c0@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#52 Weibo.HMOriginalView:0x7f80c2ec22e0.bottom == UILabel:0x7f80c2fe3ea0.bottom + 10.0>",
//    "<SnapKit.LayoutConstraint:0x7f80c2cd7190@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#45 Weibo.HMOriginalView:0x7f80c2ec22e0.bottom == Weibo.HMPictureView:0x7f80c3939800.bottom + 10.0>"
//    )
//    
//    Will attempt to recover by breaking constraint
//    <SnapKit.LayoutConstraint:0x7f80c2f44d00@/Users/lixuelin/Desktop/iPhone项目/项目/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#137 Weibo.HMPictureView:0x7f80c3939800.height == 235.0>
//    
//    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
//    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
//    2015-12-12 19:50:56.294 Weibo[1920:34117] Unable to simultaneously satisfy constraints.
//    Probably at least one of the constraints in the following list is one you don't want.
//    Try this:
//    (1) look at each constraint and try to figure out which you don't expect;
//    (2) find the code that added the unwanted constraint or constraints and fix it.
//    (
//    "<SnapKit.LayoutConstraint:0x7f80c2f78d70@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#137 Weibo.HMPictureView:0x7f80c3939800.top == UILabel:0x7f80c2fe3ea0.bottom + 10.0>",
//    "<SnapKit.LayoutConstraint:0x7f80c51494c0@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#52 Weibo.HMOriginalView:0x7f80c2ec22e0.bottom == UILabel:0x7f80c2fe3ea0.bottom + 10.0>",
//    "<SnapKit.LayoutConstraint:0x7f80c2cd7190@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#45 Weibo.HMOriginalView:0x7f80c2ec22e0.bottom == Weibo.HMPictureView:0x7f80c3939800.bottom + 10.0>"
//    )
//    
//    Will attempt to recover by breaking constraint
//    <SnapKit.LayoutConstraint:0x7f80c2f78d70@/Users/lixuelin/Desktop/iPhone项目/项目/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#137 Weibo.HMPictureView:0x7f80c3939800.top == UILabel:0x7f80c2fe3ea0.bottom + 10.0>
//    
//    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
//    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
//    2015-12-12 19:50:56.297 Weibo[1920:34117] Unable to simultaneously satisfy constraints.
//    Probably at least one of the constraints in the following list is one you don't want.
//    Try this:
//    (1) look at each constraint and try to figure out which you don't expect;
//    (2) find the code that added the unwanted constraint or constraints and fix it.
//    (
//    "<SnapKit.LayoutConstraint:0x7f80c2f78d70@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#137 Weibo.HMPictureView:0x7f80c3939800.top == UILabel:0x7f80c2fe3ea0.bottom + 10.0>",
//    "<SnapKit.LayoutConstraint:0x7f80c51494c0@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#52 Weibo.HMOriginalView:0x7f80c2ec22e0.bottom == UILabel:0x7f80c2fe3ea0.bottom + 10.0>",
//    "<SnapKit.LayoutConstraint:0x7f80c2cd7190@/Users/lixuelin/Desktop/iPhone\U9879\U76ee/\U9879\U76ee/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#45 Weibo.HMOriginalView:0x7f80c2ec22e0.bottom == Weibo.HMPictureView:0x7f80c3939800.bottom + 10.0>"
//    )
//    
//    Will attempt to recover by breaking constraint
//    <SnapKit.LayoutConstraint:0x7f80c2f78d70@/Users/lixuelin/Desktop/iPhone项目/项目/XLWeibo/Weibo/Weibo/Classes/View/Home/View/HMOriginalView.swift#137 Weibo.HMPictureView:0x7f80c3939800.top == UILabel:0x7f80c2fe3ea0.bottom + 10.0>
//    
//    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
//    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.


    
    
    // 懒加载控件
    private lazy var originalView: HMOriginalView = {
    
        let originalView = HMOriginalView()
        originalView.backgroundColor = UIColor.whiteColor()
        return originalView
        
    }()
    private lazy var retweetView:HMRetweetView = HMRetweetView()
    
    private lazy var statusToolBar: HMToolBarView = HMToolBarView()
}
